package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiBoDTO;
import cn.tedu._05weibo.pojo.entity.User;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("insert")
    public int insert(@RequestBody WeiBoDTO weiBoDTO, HttpSession session) {
        Weibo weibo = new Weibo();
        weibo.setContent(weiBoDTO.getContent());
        weibo.setCreated(new Date());
        UserVO userVO = (UserVO)session.getAttribute("user");
        if(userVO==null)
            return 2;
        weibo.setUserId(userVO.getId());
        weiboMapper.insert(weibo);
        return 1;
    }
    @GetMapping("selectIndex")
    public List<WeiboIndexVO> selectIndex() {
        return weiboMapper.selectIndex();
    }
    @GetMapping("selectById")
    public WeiboDetailVO selectById(int id){
        System.out.println("id = " + id);
        return weiboMapper.selectById(id);
    }

}
